package com.pets.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pets.entity.BountyList;
import com.pets.entity.Collect;
import com.pets.entity.DownloadRecord;
import com.pets.entity.Scoring;
import com.pets.mapper.CollectMapper;
import com.pets.mapper.DownloadRecordMapper;
import com.pets.mapper.ScoringMapper;
import com.pets.service.IScoringService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pets.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 打分表 服务实现类
 * </p>
 *
 * @author 啦啦啦
 * @since 2024-04-09
 */
@Service
public class ScoringServiceImpl extends ServiceImpl<ScoringMapper, Scoring> implements IScoringService {

    @Autowired
    private ScoringMapper scoringMapper;
    @Autowired
    private DownloadRecordMapper downloadRecordMapper;

    /**
     * 创建打分
     */
    @Override
    public Result register(Scoring scoring) {
        if(scoring.getFileId()==null||scoring.getUserId()==null||scoring.getFraction()==null){
            return Result.fail("参数有误！");
        }
        //查询是否已经打过分
        LambdaQueryWrapper<Scoring> scoringQuery = new LambdaQueryWrapper<>();
        scoringQuery.eq(Scoring::getFileId,scoring.getFileId())
                .eq(Scoring::getUserId,scoring.getUserId());
        List<Scoring> scoringList = scoringMapper.selectList(scoringQuery);
        if (scoringList!=null&&scoringList.size()>0){
            return Result.fail("不能重复打分！");
        }
        //查询下载记录
        LambdaQueryWrapper<DownloadRecord> downloadRecordQuery = new LambdaQueryWrapper<>();
        downloadRecordQuery.eq(DownloadRecord::getFileId,scoring.getFileId())
                .eq(DownloadRecord::getUserId,scoring.getUserId());
        List<DownloadRecord> downloadRecordList = downloadRecordMapper.selectList(downloadRecordQuery);
        if (downloadRecordList==null||downloadRecordList.size()<=0){
            return Result.fail("未下载不能打分！");
        }
        return scoringMapper.insert(scoring)>0?Result.success("成功！"):Result.fail("失败！");
    }
}
